package cn.iocoder.yudao.framework.pay.core.client.impl.pax.util;

import cn.hutool.http.HttpResponse;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PaxHttpUtils {

    public static HttpResponse post(String notifyUrl, Object req, String privateKey) {
        return post(notifyUrl, req, privateKey, null);
    }

    public static HttpResponse post(String url, Object req, String privateKey, String appKey) {
        final String reqVOString = JsonUtils.toJsonString(req);

        final PaxOpenSdk.PaxHeader header = PaxOpenSdk.createPaxBaseHeader(appKey);

        final String signature = PaxOpenSdk.sign(header, reqVOString, privateKey);
        header.setSignature(signature);

        log.info("http请求-开始, url = {}, req = {}, header = {}", url, reqVOString, header);
        final HttpResponse httpResponse = PaxOpenSdk.post(url, reqVOString, header);

        final int status = httpResponse.getStatus();
        if (httpResponse.isOk()) {
            log.info("http请求-成功, res = {}", httpResponse.body());
        } else {
            log.warn("http请求-失败, status = {}", status);
        }
        return httpResponse;
    }

}
